package jp.yvn.game.shooting.element;

import jp.yvn.game.shooting.util.Const;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.PointF;

/**
 * 弾幕オブジェクトクラス
 * 
 * @author Yvn_pq
 */
public class TriangleBulletEntity extends BaseBulletEntity {

	private final int TRIANGLE_APEX = 3; // 三角形の頂点数

	private final float mBuletSpeed = 1.0f; // 弾幕の移動スピード

	private final double referenceAngle = Math.toRadians(120); // 頂点間の角度

	private int eachLine = 0; // 辺ごとに弾幕数

	private int childBulletNumber = 0;

	private final PointF[] mChildBulletPoint;

	private double mRotation = 0;

	/**
	 * コンストラクタ
	 * 
	 * @param centerX
	 * @param centerY
	 * @param viewSize
	 * @param number
	 * @param rotation
	 */
	public TriangleBulletEntity(float centerX, float centerY, Point viewSize, int number, int rotation) {
		super(viewSize);
		// 3の倍数の弾幕数を設定する
		childBulletNumber = number - (number % TRIANGLE_APEX);
		if (childBulletNumber < TRIANGLE_APEX) {
			childBulletNumber = TRIANGLE_APEX;
		}
		mRotation = Math.toRadians(rotation);
		eachLine = childBulletNumber / TRIANGLE_APEX;
		mChildBulletPoint = new PointF[childBulletNumber];
		for (int i = 0; i < childBulletNumber; i++) {
			mChildBulletPoint[i] = new PointF(centerX, centerY);
		}
	}

	/**
	 * 描画します
	 * 
	 * @param canvas
	 */
	@Override
	public void draw(Canvas canvas, Bitmap bulletImg) {
		for (int i = 0; i < childBulletNumber; i++) {
			canvas.drawBitmap(bulletImg, mChildBulletPoint[i].x - Const.BULLET_BLUE_HALF_SIZE, mChildBulletPoint[i].y - Const.BULLET_BLUE_HALF_SIZE, null);
		}
		// アニメーション更新
		setAnimation();
	}

	/**
	 * 座標がオブジェクトが画面の外かどうか
	 * 
	 * @return boolean
	 */
	@Override
	public boolean isOutsideView() {
		return false;
	}

	/**
	 * アニメーションを設定します
	 */
	@Override
	public void setAnimation() {
		for (int i = 0; i < TRIANGLE_APEX; i++) {
			mChildBulletPoint[i].x += mBuletSpeed * Math.cos(mRotation + i * referenceAngle);
			mChildBulletPoint[i].y -= mBuletSpeed * Math.sin(mRotation + i * referenceAngle);
		}
		for (int i = 1; i < eachLine; i++) {
			mChildBulletPoint[i + 2].x = mChildBulletPoint[1].x + (mChildBulletPoint[0].x - mChildBulletPoint[1].x) / eachLine * i;
			mChildBulletPoint[i + 2].y = mChildBulletPoint[1].y + (mChildBulletPoint[0].y - mChildBulletPoint[1].y) / eachLine * i;
		}
		for (int i = 1; i < eachLine; i++) {
			mChildBulletPoint[i + 1 + eachLine].x = mChildBulletPoint[2].x + (mChildBulletPoint[1].x - mChildBulletPoint[2].x) / eachLine * i;
			mChildBulletPoint[i + 1 + eachLine].y = mChildBulletPoint[2].y + (mChildBulletPoint[1].y - mChildBulletPoint[2].y) / eachLine * i;
		}
		for (int i = 1; i < eachLine; i++) {
			mChildBulletPoint[i + eachLine * 2].x = mChildBulletPoint[2].x + (mChildBulletPoint[0].x - mChildBulletPoint[2].x) / eachLine * i;
			mChildBulletPoint[i + eachLine * 2].y = mChildBulletPoint[2].y + (mChildBulletPoint[0].y - mChildBulletPoint[2].y) / eachLine * i;
		}
	}
}
